<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? '添加' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  @keydown.enter.native.prevent="dataFormSubmitHandle()" label-width="120px">
      <el-form-item prop="dictValue" label="字典值">
        <el-input v-model="dataForm.dictValue" placeholder="字典值"></el-input>
      </el-form-item>
      <el-form-item prop="dictLabel" label="字典标签">
        <el-input v-model="dataForm.dictLabel" placeholder="字典标签"></el-input>
      </el-form-item>
      <!-- <el-form-item prop="sort" label="排序">
        <el-input-number v-model="dataForm.sort" controls-position="right" :min="0" label="排序"></el-input-number>
      </el-form-item> -->
      <el-form-item prop="remark" label="备注">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import servicesApi from '@/services/servicesApi'
import debounce from 'lodash/debounce'
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: '',
        dictTypeId: '',
        dictLabel: '',
        dictValue: '',
        sort: 0,
        remark: ''
      }
    }
  },
  computed: {
    dataRule() {
      return {
        dictLabel: [
          { required: true, message: '必填项不能为空', trigger: 'blur' }
        ],
        dictValue: [
          { required: true, message: '必填项不能为空', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '必填项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id, dictTypeId) {
      this.dataForm.id = id || 0
      this.dataForm.dictTypeId = dictTypeId || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      servicesApi.getDictItemInfoById(this.dataForm.id).then(res => {
        if (res.data.code !== 200) {
          return this.$message.error(res.data.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data.data
        }
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function() {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        if (this.dataForm.id) { // 修改
          // console.log('修改字典项')
          servicesApi.updateDictItem(this.dataForm).then(res => {
            if (res.data.code !== 200) {
              return this.$message.error(res.data.msg)
            }
            this.$message({
              message: '修改成功',
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        } else {  // 新增
          // console.log('新增字典项')
          servicesApi.addDictItem(this.dataForm).then(res => {
            if (res.data.code !== 200) {
              return this.$message.error(res.data.msg)
            }
            this.$message({
              message: '添加成功',
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
